package cn.tedu.personal_patient.information.services.impl;


import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.personal_patient.information.dao.persist.repository.IStaffRepository;
import cn.tedu.personal_patient.information.pojo.entity.Staff;
import cn.tedu.personal_patient.information.pojo.param.StaffAvatarParam;
import cn.tedu.personal_patient.information.pojo.param.StaffParam;
import cn.tedu.personal_patient.information.pojo.param.StaffUpdateInfoParam;
import cn.tedu.personal_patient.information.pojo.vo.StaffVO;
import cn.tedu.personal_patient.information.services.IStaffService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Service
public class StaffServiceImpl implements IStaffService {

    @Autowired
    private IStaffRepository staffRepository;

    @Override
    public List<StaffVO> listById(CurrentPrincipal currentPrincipal) {
        return staffRepository.listById(currentPrincipal.getId());
    }
    @Override
    public StaffVO getById(Long id) {
        return staffRepository.getById(id);
    }

    /*@Override
    public void updateCardImgeUrl(Long id, String cardImgeUrl) {
        Staff staff= new Staff();
        staff.setCardImgeUrl(cardImgeUrl);
        staff.setId(id);
        int rows = staffRepository.updateById(staff);
        if (rows != 1) {
            String message = "修改工作证失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updatePicImgeRul(Long id, String picImgeRul) {
        System.out.println("id = " + id + ", picImgeRul = " + picImgeRul);
        Staff staff= new Staff();
        staff.setId(id);
        staff.setPicImgeRul(picImgeRul);
        int rows = staffRepository.updateById(staff);
        if (rows != 1) {
            String message = "修改头像失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updatePhone(Long id, String phone) {
        int count = staffRepository.countByPhoneAndNotId(phone, id);
        if (count > 0) {
            String message = "修改手机号码失败，手机号码已经被占用！";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        Staff staff= new Staff();
        staff.setId(id);
        staff.setPhone(phone);
        System.out.println("staff========="+staff);
        int rows = staffRepository.updateById(staff);
        if (rows != 1) {
            String message = "修改手机号码失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void updateHomeAddress(Long id, String homeAddress) {
        Staff staff= new Staff();
        staff.setId(id);
        staff.setHomeAddress(homeAddress);
        int rows = staffRepository.updateById(staff);
        if (rows != 1) {
            String message = "修改家庭地址失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
*/
    @Override
    public void updateById(@ApiIgnore CurrentPrincipal currentPrincipal, StaffParam staffParam) {
        Staff staff=new Staff();

        BeanUtils.copyProperties(staffParam,staff);
        staff.setId(currentPrincipal.getId());
        int rows = staffRepository.updateById(staff);
        if (rows!=1){
            String message = "修改失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void avatar(CurrentPrincipal currentPrincipal, StaffAvatarParam staffAvatarParam) {
        Staff staff=new Staff();
        BeanUtils.copyProperties(staffAvatarParam,staff);
        staff.setId(currentPrincipal.getId());
        int rows = staffRepository.avatar(staff);

        if (rows!=1){
            String message = "修改失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

}
